package com.server.strategy;

import com.dao.dashboard.DashboardsMapper;
import com.dao.strategy.StrategysBdMapper;
import com.dao.strategy.StrategysChartMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.model.bo.DashOrder;
import com.model.bo.Obj;
import com.model.po.Strategys;
import com.model.po.StrategysData;
import com.model.pojo.RepCode;
import com.model.pojo.RepEntity;
import com.model.vo.configVo.ObjectInfo;
import com.model.vo.configVo.StrategysInfo;
import com.util.ScreenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StrategysService {
    @Autowired
    ScreenUtil screenUtil;
    @Autowired
    StrategysBdMapper strategysBdMapper;
    @Autowired
    StrategysChartMapper strategysChartMapper;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    DashboardsMapper dashboardsMapper;
    /*
   保存数据源策略
    */
    public RepEntity addStrategys(StrategysInfo strategysInfo){
        Strategys strategys = new Strategys();
        BeanUtils.copyProperties(strategysInfo, strategys);
        String scr = screenUtil.screensUtil(strategysInfo.getRule(), "", "").getRet();
        strategys.setRule(scr);
        strategys.setRuleStr(String.valueOf(strategysInfo.getRule()));

        String type = strategysInfo.getType();
        if ("base".equals(type)){
            strategysBdMapper.addStrategys(strategys);
        }else {
            strategysChartMapper.addStrategys(strategys);
        }

        return new RepEntity(RepCode.success, strategys.getId());
    }

    /*
    更新数据源策略
     */
    public RepEntity updateStrategys(StrategysInfo strategysInfo){
        Strategys strategys = new Strategys();
        BeanUtils.copyProperties(strategysInfo, strategys);
        String scr = screenUtil.screensUtil(strategysInfo.getRule(), "", "").getRet();
        strategys.setRule(scr);
        String ruleStr = null;
        try {
            ruleStr = objectMapper.writeValueAsString(strategysInfo.getRule());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        strategys.setRuleStr(ruleStr);
        String type = strategysInfo.getType();
        if ("base".equals(type)) {
            strategysBdMapper.updataStrategys(strategys);
        }else {
            strategysChartMapper.updataStrategys(strategys);
        }
        return new RepEntity(RepCode.success);
    }

    /*
    删除数据源策略
     */
    public RepEntity delDbStrategys(List<Integer> idList){
        strategysBdMapper.delStrategys(idList);
        Iterator isList = idList.iterator();
        while (isList.hasNext()){
            int id = (int) isList.next();
            strategysBdMapper.delObject(id);
        }
        return new RepEntity(RepCode.success);
    }

    /*
    删除图表策略
     */
    public RepEntity delChartStrategys(List<Integer> idList){
        strategysChartMapper.delStrategys(idList);
        Iterator isList = idList.iterator();
        while (isList.hasNext()){
            int id = (int) isList.next();
            strategysChartMapper.delObject(id);
        }
        return new RepEntity(RepCode.success);
    }

    /*
    查询数据源策略列表
     */
    public RepEntity getDbStrategys(int DbId){

        List<StrategysData> strategysDataList = new ArrayList<>();
        /*
        获取id列表
         */
        List<Integer> idList = strategysBdMapper.getStrIdList(DbId);
        Iterator isList = idList.iterator();
        while (isList.hasNext()) {
            int id = (int) isList.next();
            List<DashOrder> userGroupNameList = new ArrayList<>();
            List<DashOrder> userNameList = new ArrayList<>();
            StrategysData strategysDatas = new StrategysData();

            Strategys strategys = strategysBdMapper.getStrategys(id);  //策略的基本信息
            List<String> types = strategysBdMapper.getStrategysType(id);
            Iterator isTypeList = types.iterator();
            while (isTypeList.hasNext()) {
                String type = (String) isTypeList.next();
                if ("0".equals(type)) {
                    userGroupNameList = strategysBdMapper.getUserGroupIdAndName(id);
                    System.out.println("userG:" + userGroupNameList);
                } else {
                    userNameList = strategysBdMapper.getUserIdAndName(id);
                    System.out.println("user" + userNameList);
                }
            }
            strategysDatas.setStrategies(strategys);
            strategysDatas.setUserGroupName(userGroupNameList);
            strategysDatas.setUserName(userNameList);
            strategysDataList.add(strategysDatas);
            System.out.println("strate:" + strategysDataList);
        }
        return new RepEntity(RepCode.success, strategysDataList);
    }

    /*
    添加用户或用户组
     */
    public RepEntity addObject(ObjectInfo objectInfo){
        String type = objectInfo.getType();
        int stId = objectInfo.getStId();
        if ("base".equals(type)){
            strategysBdMapper.delObject(stId);
        }else if ("chart".equals(type)){
            strategysChartMapper.delObject(stId);
        }else {
            dashboardsMapper.delObject(stId);
        }
        List objs = objectInfo.getObj();
        Iterator isObj = objs.iterator();
        while (isObj.hasNext()) {
            Obj obj = (Obj) isObj.next();
            String obType = obj.getObjectType();
            int obId = obj.getObId();
            System.out.println("obId:" + obId);
            if ("base".equals(type)) {
                strategysBdMapper.addObject(stId, obType, obId);
            } else if ("chart".equals(type)){
                strategysChartMapper.addObject(stId, obType, obId);
            }else {
                dashboardsMapper.addObject(stId, obType, obId);
            }
        }
        return new RepEntity(RepCode.success);
    }


    /*
    查询图表策略列表
     */
    public RepEntity getChartStrategys(int chartId){
        System.out.println(",,,");
        List<StrategysData> strategysDataList = new ArrayList<>();
        /*
        获取id列表
         */
        List<Integer> idList = strategysChartMapper.getStrIdList(chartId);
        Iterator isList = idList.iterator();
        while (isList.hasNext()) {
            int id = (int) isList.next();
            List<DashOrder> userGroupNameList = new ArrayList<>();
            List<DashOrder> userNameList = new ArrayList<>();
            StrategysData strategysDatas = new StrategysData();

            Strategys strategys = strategysChartMapper.getStrategys(id);  //策略的基本信息
            List<String> types = strategysChartMapper.getStrategysType(id);
            Iterator isTypeList = types.iterator();
            while (isTypeList.hasNext()) {
                String type = (String) isTypeList.next();
                if ("0".equals(type)) {
                    userGroupNameList = strategysChartMapper.getUserGroupIdAndName(id);
                } else {
                    userNameList = strategysChartMapper.getUserIdAndName(id);
                }
            }
            strategysDatas.setStrategies(strategys);
            strategysDatas.setUserGroupName(userGroupNameList);
            strategysDatas.setUserName(userNameList);
            strategysDataList.add(strategysDatas);
        }
        return new RepEntity(RepCode.success, strategysDataList);
    }
}
